home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre2.z / postgre2 / doc / implementation / planner / 5.perf < prev    next >
Encoding:
Text File  |  1992-08-27  |  20.4 KB  |  547 lines

  1. .sh 1 "PERFORMANCE OF THE POSTGRES OPTIMIZER" 5
  2. .pp
  3. This section describes how we went about validating the POSTGRES optimizer.
  4. It also presents and discusses our results.
  5. .sh 2 "Testing The Optimizer"
  6. .pp
  7. To evaluate the POSTGRES optimizer's performance as well as
  8. assess the credibility of its cost formulas, we could do the following.
  9. We could measure the amount of time required to execute
  10. various query plans, and then we could compare these measurements with
  11. the optimizer's predicted costs.
  12. Ideally, the predicted cost will be identical
  13. to the actual measured cost.
  14. However, this is an unrealistic expectation since optimizer
  15. cost formulas are merely estimates.
  16. A more attainable goal is for the optimizer to select the true
  17. optimal plan in a large majority of cases.
  18. This will at least show that in almost all circumstances, a selected plan
  19. is the best plan not only according to the optimizer but also in reality.
  20. .pp
  21. The tests described above illustrate how we would have wanted to validate
  22. the optimizer.
  23. However, at the time when we wanted to run these performance tests,
  24. measuring the amount of time required to execute a query plan
  25. was impossible because other parts of POSTGRES
  26. had not been fully implemented. 
  27. As an alternative, we compared POSTGRES plans with
  28. query plans selected by the optimizer of commercial INGRES [KOOI82],
  29. which also happens to use an enumerative planning algorithm.
  30. The assumption behind this is that the INGRES optimizer selects
  31. \*(lqcorrect\*(rq plans.
  32. Therefore, if a plan selected by POSTGRES is equivalent to a plan selected by
  33. INGRES, (for the same query under similar conditions), then
  34. the POSTGRES optimizer has also generated a \*(lqcorrect\*(rq plan.
  35. Although this may not always be a valid assumption, it is probably
  36. a very good one since the INGRES optimizer has been tested, tuned, and
  37. used widely; and tests have shown that it is
  38. \*(lqextremely good\*(rq [ROWE86].
  39. Furthermore, comparing POSTGRES
  40. and INGRES query plans at least allowed us to validate our optimizer against
  41. something other than our intuition as to which plans \fIshould\fR be
  42. selected.
  43. .pp
  44. Commercial INGRES was chosen as a basis for our comparisons because
  45. it is ideal in several respects.
  46. First of all, it has a feature that allows users to examine
  47. plans selected by the optimizer.
  48. By issuing the command \*(lq\fBset qep\fR,\*(rq
  49. plan trees for all subsequent join queries will be printed on
  50. the terminal monitor, easily allowing us to make comparisons between
  51. INGRES and POSTGRES plans.
  52. In addition, both POSTGRES and INGRES
  53. store within database catalogs statistical information that can be used to
  54. compute more realistic operator selectivities.
  55. In INGRES, these statistics are generated and subsequently updated
  56. using the \*(lq\fBoptimizedb\fR\*(rq command, while POSTGRES maintains
  57. them using system demons.
  58. An example of how this extra information would be put to use is the following.
  59. Suppose a relation, \fIr\fR, has 100 tuples but only 10 distinct
  60. values in attribute, \fIr.f\fR.
  61. Because the number
  62. of distinct values is a better indicator of the distribution of data
  63. within \fIr.f\fR, a clause like \fIr.f = 1\fR would have a
  64. selectivity of $1 over 10$ rather than $1 over 100$, as a result of using
  65. this extra information.
  66. In both systems, we made use of this and other statistical information
  67. to generate more accurate cost estimations.
  68. As a result, fair comparisons could be made between the two optimizers.
  69. .pp
  70. However, the POSTGRES optimizer is \fInot\fR identical to the INGRES
  71. optimizer, and consequently, in certain situations, INGRES selected
  72. a plan that POSTGRES did not even consider.
  73. In these cases, it was impossible to determine whether POSTGRES had selected
  74. the optimal path from among those plans it had considered; but as
  75. will become evident in the next subsection, we were still able to draw
  76. some interesting conclusions.
  77. One situation where this occurs relates to the manner in which both 
  78. systems treat secondary indices.
  79. Figure 5.1 illustrates how a secondary index is generally used.
  80. Given the key(s) of the index, the system locates within
  81. the secondary index relation a unique tuple identifier (tid).
  82. It then uses the tid to directly retrieve an appropriate tuple from the 
  83. corresponding indexed relation.
  84. .(z
  85. .hl
  86. .GS
  87. file figure5.1
  88. .GE
  89. .sp
  90. .ce 2
  91. \fBFigure 5.1\fR
  92. Using secondary indices
  93. .hl
  94. .)z
  95. The POSTGRES query plan tree corresponding to such a secondary index scan is
  96. shown in figure 5.2a, while the INGRES tree is shown in figure 5.2b.
  97. .(z
  98. .hl
  99. .GS
  100. file figure5.2
  101. .GE
  102. .sp
  103. .ce 2
  104. \fBFigure 5.2\fR
  105. Plan trees for secondary index scans in POSTGRES and INGRES
  106. .hl
  107. .)z
  108. A join between the tidp field in EMPINDEX and tid in EMP is required because
  109. INGRES literally treats secondary indices as relations, given that relations
  110. are used to implement them.
  111. In this particular case,
  112. although the two trees are different in appearance, they are both
  113. processed in the manner shown in figure 5.1.
  114. The only difference is that joining of tids
  115. is implicit in the POSTGRES indexscan node.
  116. .pp
  117. It will not always be the case that there will be a POSTGRES tree
  118. equivalent to an INGRES tree because by treating secondary indices as
  119. relations, further strategies are available to the INGRES optimizer.
  120. In figure 5.2, the index relation, EMPINDEX, is joined directly with 
  121. its corresponding indexed relation, EMP.
  122. POSTGRES will only use a secondary index in this manner.
  123. INGRES, however, may choose to sort the result of a secondary scan, or
  124. it may join the result
  125. with a relation other than the corresponding indexed relation.
  126. Figure 5.3 illustrates these two situations.
  127. Examples where this generality is advantageous will be discussed in the
  128. next subsection.
  129. .(z
  130. .hl
  131. .GS
  132. file figure5.3
  133. .GE
  134. .sp
  135. .ce 2
  136. \fBFigure 5.3\fR
  137. Other processing strategies using secondary indices in INGRES
  138. .hl
  139. .)z
  140. .pp
  141. Another disadvantage of testing our optimizer against INGRES's is that
  142. INGRES is a conventional database system that does not support user-defined
  143. operators and access methods or nested-attribute queries.
  144. As a result, we could only test standard operators, access methods, and queries.
  145. Fortunately, this drawback was insignificant.
  146. In POSTGRES, costs are computed using
  147. formulas as well as operator selectivities.
  148. The latter is supplied by the user.
  149. In other words, it is a parameter that is not inherent within the optimizer
  150. and thus cannot be manipulated or tuned (except by the user who supplied the
  151. routines).
  152. Consequently, provided selectivities relevant to a single operator and storage 
  153. structure are accurate, one of each was sufficient for testing purposes.
  154. It would have been nice to illustrate the generality of our optimizer by using
  155. non-standard operators in our tests, but even standard operators and access 
  156. methods in POSTGRES are implemented as if they were user-defined.
  157. Thus, no generality was lost in using a conventional operator and access
  158. method in our tests.
  159. .pp
  160. The single operator and access method used were the equality operator,
  161. since it is a
  162. mergesortable operator, and an ISAM storage structure [HELD75a].
  163. To build an ISAM storage structure, tuples must first be sorted
  164. into data pages.
  165. Then, a multi-level directory is constructed indicating the high key value
  166. stored within each page.
  167. Such a structure provides quick access when used with the following operators:
  168. .(l
  169. =, <, $<=$, >, and $>=$.
  170. .)l
  171. The directory is analogous to the internal nodes of a B-tree except that 
  172. once it has been built, the directory remains static.
  173. Therefore, when a data page is filled, rather than
  174. splitting nodes to maintain a balanced tree,
  175. overflow pages are created and linked to the filled data page.
  176. If a large number of overflow pages are created, finding a tuple within a page
  177. requires an inefficient linear search through the primary page 
  178. as well as its overflow pages.
  179. So, given a choice between an ISAM storage structure
  180. and a B-tree, a user would probably choose a B-tree.
  181. However, we could not use B-trees in our tests because the version of 
  182. INGRES that we used only supported ISAM and
  183. hash access methods.
  184. Forced to choose between the two, we chose ISAM because
  185. there is a greater overhead
  186. associated with searching through an ISAM directory, making them more 
  187. interesting than hash tables.
  188. .pp
  189. Using an ISAM access method does have its disadvantages, though.
  190. Although tuples in an ISAM structure are initially sorted when
  191. the structure is built, the tuples are not maintained in sort order.
  192. As a result, we could not test the POSTGRES optimizer feature that takes
  193. advantage of access methods like B-trees
  194. to eliminate sort steps required to order a 
  195. user-specified sort result or tuples for a merge-sort join.
  196. However, although merge-sorting on a tuple by tuple basis is not possible,
  197. a variation of merge-sort can be performed on 
  198. a page by page basis since the range of values within each page is known.
  199. INGRES, in fact, does this.
  200. In contrast, the POSTGRES optimizer does not, and as a result, differences
  201. arose in our performance tests.
  202. We chose not to account
  203. for partial index sorts because few access methods have this unusual
  204. characteristic.
  205. Moreover, as already alluded to, users will likely opt for access 
  206. methods like B-trees, which always maintain their records in sort order.
  207. In other words, this feature would probably not
  208. be employed very often, had we implemented it.
  209. This should be kept in mind when differences arise between POSTGRES and
  210. INGRES plans in the next subsection.
  211. .pp
  212. With respect to nested-attribute queries, not being able to test these either is
  213. also of minimal importance.
  214. As discussed in section 3.5, relations materialized from queries
  215. embedded within data fields will generally be small, and as a result, the
  216. cost of executing any subplan corresponding to a nested portion of a query
  217. will also be small.
  218. Therefore, it is less important if the true optimal subplan is not selected
  219. while optimizing this portion of a query.
  220. .pp
  221. In testing the optimizer, we used version 2.1 of INGRES running on 
  222. a VAX 11/780.
  223. To simulate the INGRES optimizer as closely as possible, 
  224. we had to determine values for two system-dependent parameters that affect
  225. cost estimations.
  226. One of these is the page size used by the database.
  227. This is required because in estimating the cost of a sort, the 
  228. optimizer must determine the number
  229. of pages occupied by the tuples to be sorted.
  230. By multiplying the number of tuples in an INGRES relation by the width of
  231. a tuple (including space for internal page pointers) and
  232. dividing by the number of pages in the relation,
  233. it turns out that INGRES uses 2K pages.
  234. .pp
  235. Determining a value for the second parameter, \fIW\fR, which relates
  236. I/O to CPU cost, was less straightforward.
  237. Assuming that it takes 30 milliseconds and about 2000 instructions of
  238. operating system overhead to
  239. read an I/O page, while
  240. manipulating a tuple only consumes about 200 CPU instructions,
  241. \fIW\fR is in the range of 0.03 to 0.1 [DEWI84].
  242. For the queries used in our tests, using various values within
  243. this range did not affect the plan choice except in a few situations
  244. where a merge-sort join was chosen over nested iteration.
  245. This occurred when \fIW\fR was close to 0.03, a value that apparently minimized
  246. the cost of CPU-intensive tasks like sorting.
  247. In these situations, the INGRES optimizer always selected a nested
  248. iteration join.
  249. Thus, to insure compatibility with INGRES plans, we assigned \fIW\fR the value 
  250. 0.065, arbitrarily choosing a value in the middle of our original range.
  251. .sh 2 "Tests Performed"
  252. .pp
  253. For testing purposes, we used the following three relations:
  254. EMP, DEPT, and WATER.
  255. Schemas and relevant statistics for these relation are shown in figure 5.4.
  256. .(z
  257. .hl
  258. .sz \n(ep
  259. .nf
  260. \fBRELATIONS :\fR
  261. .sp
  262. EMP ( name = c10, salary = i4, manager = c10, age = i4, dept = c10 )
  263. .in +0.5i
  264. # pages : 600 (unindexed)
  265. # tuples : 30,000
  266. # distinct values in name : 30,000
  267. # distinct values in dept = 1000
  268. .in -0.5i
  269. .sp
  270. DEPT ( dname = c10, floor = i4, budget = i4 )
  271. .in +0.5i
  272. # pages : 10 (unindexed), 14 (primary ISAM index on floor)
  273. # tuples : 1000
  274. # distinct values in dname : 1000
  275. # distinct values in floor : 9
  276. .in -0.5i
  277. .sp
  278. WATER ( cid = i4, floor = i4 )
  279. .in +0.5i
  280. # pages : 1 (unindexed)
  281. # tuples : 50
  282. # distinct values in cid : 9
  283. # distinct values in floor : 9
  284. .in -0.5i
  285. .sp 2
  286. \fBSECONDARY INDICES : \fR
  287. .sp
  288. EMPINDEX ( ISAM on dept ) :
  289. .in +0.5i
  290. # pages : 253
  291. # tuples : 30,000
  292. .in -0.5i
  293. .sp
  294. DEPTINDEX ( ISAM on floor ) :
  295. .in +0.5i
  296. # pages : 8
  297. # tuples : 1000
  298. .in -0.5i
  299. .sp
  300. DEPTINDEX ( ISAM on dept ) :
  301. .in +0.5i
  302. # pages : 11
  303. # tuples : 1000
  304. .in -0.5i
  305. .fi
  306. .sz \n(pp
  307. .sp
  308. .ce 2
  309. \fBFigure 5.4\fR
  310. Schemas and statistics for test relations
  311. .hl
  312. .)z
  313. First, we ran the following four two-way join queries on EMP and DEPT:
  314. .(l
  315. A:  \fBretrieve\fR (EMP.name, EMP.age, DEPT.all) \fBwhere\fR
  316.     EMP.dept = DEPT.dname
  317.  
  318. B:  \fBretrieve\fR (EMP.name, EMP.age, DEPT.dname, DEPT.budget) \fBwhere\fR
  319.     EMP.dept = DEPT.dname \fBand\fR
  320.     DEPT.floor = 1
  321.  
  322. C:  \fBretrieve\fR (EMP.age, EMP.dname, DEPT.floor, DEPT.budget) \fBwhere\fR
  323.     EMP.dept = DEPT.dname \fBand\fR
  324.     EMP.name = \*(lqDiamond\*(rq
  325.  
  326. D:  \fBretrieve\fR (EMP.age, DEPT.dname, DEPT.budget) \fBwhere\fR
  327.     EMP.dept = DEPT.dname \fBand\fR
  328.     DEPT.floor = 1 \fBand\fR
  329.     EMP.name = \*(lqDiamond\*(rq
  330. .)l
  331. To illustrate that the optimizer chooses appropriate strategies
  332. under varying conditions, we ran the four queries for non-indexed relations as
  333. well as relations with primary and secondary indices defined on relevant
  334. attributes.
  335. Indices were selected so as to illustrate the combined effects of relation
  336. sizes, distribution of values, and secondary index overhead in determining
  337. a query's optimal plan.
  338. The results of running queries A-D using various
  339. indices are shown in table 5.1.
  340. .(z
  341. .hl
  342. .sz \n(ep
  343. .nf
  344. .in +1i
  345. A:  \fBretrieve\fR (EMP.name, EMP.age, DEPT.all) \fBwhere\fR
  346.     EMP.dept = DEPT.dname
  347.  
  348. B:  \fBretrieve\fR (EMP.name, EMP.age, DEPT.dname, DEPT.budget) \fBwhere\fR
  349.     EMP.dept = DEPT.dname \fBand\fR
  350.     DEPT.floor = 1
  351.  
  352. C:  \fBretrieve\fR (EMP.age, EMP.dname, DEPT.floor, DEPT.budget) \fBwhere\fR
  353.     EMP.dept = DEPT.dname \fBand\fR
  354.     EMP.name = \*(lqDiamond\*(rq
  355.  
  356. D:  \fBretrieve\fR (EMP.age, DEPT.dname, DEPT.budget) \fBwhere\fR
  357.     EMP.dept = DEPT.dname \fBand\fR
  358.     DEPT.floor = 1 \fBand\fR
  359.     EMP.name = \*(lqDiamond\*(rq
  360. .in -1i
  361. .fi
  362. .sp 
  363. .TS
  364. center box;
  365. c|c|c|c|c.
  366. Storage Structure    Query A    Query B    Query C    Query D
  367. =
  368. no indices    Plan (a) \(dg    Plan (a)    Plan (c)    Plan (c)
  369. _
  370. secondary index on DEPT (floor)        Plan (a)        
  371. _
  372. primary index on DEPT (floor)        Plan (b)        Plan (d)
  373. _
  374. secondary index on EMP (dept)    Plan (c)        Plan (e)
  375. _
  376. primary index on DEPT (floor) &        Plan (b)        Plan (g)
  377. secondary index on DEPT (dname)
  378. _
  379. secondary index on EMP (dept)    Plan (f)        Plan (c)
  380. _
  381. secondary index on DEPT (dname) &    Plan (f)
  382. secondary index on EMP (dept)
  383. .TE
  384. .sp
  385. .sz \n(fp
  386. $" " sup \(dg$ See figure 5.5
  387. .sz \n(pp
  388. .sp
  389. .ce 2
  390. \fBTable 5.1\fR
  391. Results of executing queries A-D using various storage structures
  392. .hl
  393. .)z
  394. .(z
  395. .GS
  396. file figure5.6
  397. .GE
  398. .sz \n(fp
  399. .nf
  400. MS = merge-sort
  401. NL = nestloop
  402. .fi
  403. .sz \n(pp
  404. .ce 2
  405. \fBFigure 5.5\fR
  406. Comparison of selected POSTGRES and INGRES plans
  407. .hl
  408. .)z
  409. For the fourteen executions shown, when indices were defined, in all but
  410. one case both
  411. POSTGRES and INGRES made similar decisions as to whether or not the indices
  412. should be used, and except in cases where INGRES took advantage of
  413. a partial ISAM sort, both selected equivalent join strategies.
  414. .pp
  415. Figure 5.5 gives a pictorial representation of the seven different
  416. corresponding plan trees mentioned in table 5.1.
  417. For each pair, the POSTGRES tree is shown on the left while the INGRES tree
  418. is on the right.
  419. Both pairs of plans (a) and (b) correspond to merge-sort joins,
  420. and both plan (b)'s
  421. scan DEPT using a primary index defined on \*(lqfloor.\*(rq
  422. In plans (c) and (d), POSTGRES uses nested iteration, scanning EMP first
  423. because the highly restrictive clause, 
  424. .(l
  425. EMP.name = \*(lqDiamond,\*(rq
  426. .)l
  427. applies in these cases.
  428. INGRES, however, uses a slight variation of nested iteration in both
  429. (c) and (d).
  430. EMP is scanned and sorted into a temporary before it is nest iterated with 
  431. DEPT.
  432. Sorting only the inner relation on the inner join attribute alleviates having
  433. to scan the entire inner join relation on every iteration because
  434. the scan halts after a value greater than the current outer
  435. join attribute has been reached.
  436. In certain cases, this can be helpful;
  437. however, in these cases, there is really no advantage because only
  438. a single tuple results after scanning EMP.
  439. In fact, these INGRES and POSTGRES plans are equivalent
  440. in cost because the cost of the extra sort step in the INGRES trees is zero,
  441. and in both cases, EMP and DEPT only need to be scanned once.
  442. .pp
  443. POSTGRES's plan (e) is similar to INGRES's except INGRES takes advantage
  444. of an ISAM's partial sort and
  445. thus uses a slight variation of a merge-sort join with EMP and DEPTINDEX.
  446. Again, only one tuple qualifies after scanning EMP, resulting in a sort cost
  447. of zero.
  448. Ignoring the sort step, the two plans are actually equivalent.
  449. .pp
  450. Plan (f) is an example where INGRES's treatment of an access method's
  451. partial ordering is helpful.
  452. In this particular case, INGRES takes advantage of the feature
  453. to join EMPINDEX with DEPT.
  454. The result of this join is then
  455. sorted on the tidp field so EMP can be scanned in page order.
  456. According to POSTGRES cost estimates, the INGRES plan is better by about a
  457. factor of four to five.
  458. It is significantly better than the POSTGRES plan partly because
  459. of INGRES's treatment of secondary indices, but more so due to
  460. INGRES accounting for an ISAM's partial sort.
  461. Therefore, the fact that INGRES was significantly better in this case is
  462. misleading for the reasons discussed in the previous subsection.
  463. .pp
  464. The last plan pair (g) illustrates the one case where different indices
  465. are selected by POSTGRES and INGRES.
  466. INGRES chooses to use
  467. the index defined on \*(lqfloor,\*(rq while POSTGRES chooses the
  468. index defined on \*(lqdept.\*(rq
  469. POSTGRES chose the plan it did because according to its cost estimates
  470. the selected plan was a mere 0.27% better than a plan equivalent to that chosen
  471. by INGRES.
  472. Thus, although the two optimizers chose different plans, the difference is
  473. very minor.
  474. .pp
  475. The set of tests just described indicate that differences between selected
  476. INGRES and POSTGRES plans are rather subtle, resulting in minor variations,
  477. except in one case where the difference is minimized by other circumstances.
  478. Additional strategies employed by INGRES
  479. resulted in greater differences when the following three-way join:
  480. .(l
  481. \fBretrieve\fR (EMP.name, DEPT.floor, WATER.cid) \fBwhere\fR
  482.     EMP.dept = DEPT.dname \fBand\fR
  483.     DEPT.floor = WATER.floor
  484. .)l
  485. was run on several different relation configurations.
  486. The results are shown in table 5.2.
  487. .(z
  488. .hl
  489. .GS
  490. file table5.2
  491. .GE
  492. .sp
  493. .ce 2
  494. \fBTable 5.2\fR
  495. Results of executing a three-way join on various storage structures
  496. .hl
  497. .)z
  498. .(z
  499. .hl
  500. .GS
  501. file table5.3
  502. .GE
  503. .sp
  504. .ce 3
  505. \fBTable 5.2\fR
  506. (continued)
  507. .hl
  508. .)z
  509. .pp
  510. Plans (1), (2), and (4) in table 5.2 illustrate another strategy
  511. employed by INGRES that POSTGRES does not consider.
  512. In these plans, INGRES first sorts
  513. DEPT on \*(lqdname\*(rq and then nest iterates the result with WATER.
  514. Since a join preserves the sort order of its outer join relation, the join
  515. result is still sorted in \*(lqdname\*(rq order and thus can be used
  516. in a merge with EMP.
  517. This is advantageous because it may be cheaper to sort DEPT prior to
  518. its join with WATER, since more tuples may have to be sorted after the join.
  519. Other differences were already discussed in reference to the previous set
  520. of tests and will not be repeated.
  521. .pp
  522. To assess the magnitude of differences in POSTGRES and INGRES plans
  523. for the four cases shown, the costs of all INGRES plans were calculated using 
  524. POSTGRES cost estimation formulas.
  525. These computed costs were compared with the costs of corresponding
  526. POSTGRES plans,
  527. and the percentage differences are shown in table 5.2.
  528. The results show that INGRES was better than POSTGRES in all four cases.
  529. However, INGRES was only significantly better in one case, and this was
  530. a result of INGRES accounting for a partial ISAM ordering.
  531. In all other cases, the additional strategies considered by INGRES
  532. resulted in no more than a 13.8% difference.
  533. .pp
  534. To summarize, in spite of differences between the POSTGRES and INGRES
  535. optimizers, we were able to draw two promising conclusions from our
  536. performance tests.
  537. For the most part, both systems selected similar strategies
  538. for queries involving a single join.
  539. Assuming that the INGRES optimizer is correct, these results indicate that 
  540. POSTGRES selects true optimal plans.
  541. For queries for which INGRES was able to capitalize on a broader range
  542. of strategies, INGRES did not perform considerably better.
  543. Thus, not having considered all these other processing strategies,
  544. which would have added further complexity to
  545. our implementation effort, did not dramatically affect the overall performance
  546. of our optimizer.
  547.